package com.transferwallet.sdk.service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.transferwallet.sdk.entity.BusinessResidentReq;
import com.transferwallet.sdk.entity.CashOutForSelfReq;
import com.transferwallet.sdk.entity.ConfirmPayReq;
import com.transferwallet.sdk.entity.DrawbackReq;
import com.transferwallet.sdk.entity.LoadWithRepayPlanReq;
import com.transferwallet.sdk.entity.OrderChangeStatusReq;
import com.transferwallet.sdk.entity.OrderSubmissionReq;
import com.transferwallet.sdk.entity.QueryAccountingDetailsReq;
import com.transferwallet.sdk.entity.QueryBatchDetailsReq;
import com.transferwallet.sdk.entity.QueryBatchEssentialsReq;
import com.transferwallet.sdk.entity.QueryOpenAccountReq;
import com.transferwallet.sdk.entity.QueryVritualAccountsReq;
import com.transferwallet.sdk.entity.SingleTransferReq;
import com.transferwallet.sdk.entity.UrlConst;
import com.transferwallet.sdk.util.HttpUtils;
import com.transferwallet.sdk.util.RequestResult;
import com.transferwallet.sdk.util.SignatrueUtils;

/**
 * 
 * Title: TransferWalletService.java Description: sdk服务类
 * 
 * @author llj
 * @date 2019年8月7日
 */
@Slf4j
public class TransferWalletService {


	/**
	 * 2.1 商户入网
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param businessResidentReq
	 * @param filePath
	 * @return
	 */
	public String businessResident(String memberCode, String privateKey, String serviceHost,
			BusinessResidentReq businessResidentReq, String filePath) {
		File file = new File(filePath);

		if (!file.exists()) {
			return "本地文件不存在";
		}

		Map<String, Object> params = new HashMap<>();
		params.put("memCode", businessResidentReq.getMemCode());
		params.put("batchNo", businessResidentReq.getBatchNo());
		params.put("file", file);

		// 签名
		String text = new StringBuilder().append(businessResidentReq.getBatchNo())
				.append(businessResidentReq.getMemCode()).toString();
		String sign = SignatrueUtils.getSignToString(text, privateKey);

		params.put("sign", sign);

		String result = HttpUtils.doPost(serviceHost + UrlConst.businessResident, params);

		return result;
	}

	/**
	 * 2.1 商户入网
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param businessResidentReq
	 * @param filePath
	 * @return
	 */
	public String businessResident(String memberCode, String privateKey, String serviceHost,
			BusinessResidentReq businessResidentReq, File file) {

		if (!file.exists()) {
			return "本地文件不存在";
		}

		Map<String, Object> params = new HashMap<>();

		params.put("memCode", businessResidentReq.getMemCode());
		params.put("batchNo", businessResidentReq.getBatchNo());
		params.put("file", file);
		String text = new StringBuilder().append(businessResidentReq.getBatchNo())
				.append(businessResidentReq.getMemCode()).toString();
		String sign = SignatrueUtils.getSignToString(text, privateKey);
		params.put("sign", sign);

		String result = HttpUtils.doPost(serviceHost + UrlConst.businessResident, params);

		return result;
	}

	/**
	 * 2.2 资金划转
	 * 
	 * @param memberCode  商户编码
	 * @param privateKey  私钥
	 * @param serviceHost 服务host
	 * @param batchNo     批号
	 * @param filePath    文件路径
	 * @return
	 */
	public String batchTransfer(String memberCode, String privateKey, String serviceHost,
			LoadWithRepayPlanReq loadWithRepayPlanReq, String transferFilePath) {
		File file = new File(transferFilePath);

		if (!file.exists()) {
			return "本地文件不存在";
		}

		Map<String, Object> params = new HashMap<>();
		params.put("memCode", loadWithRepayPlanReq.getMemCode());
		params.put("batchNo", loadWithRepayPlanReq.getBatchNo());
		params.put("fundAccountNo", loadWithRepayPlanReq.getFundAccountNo());
		params.put("fundAccountType", loadWithRepayPlanReq.getFundAccountType());
		params.put("file", file);
		// 签名
		String text = new StringBuilder().append(loadWithRepayPlanReq.getBatchNo())
				.append(loadWithRepayPlanReq.getFundAccountNo()).append(loadWithRepayPlanReq.getFundAccountType())
				.append(loadWithRepayPlanReq.getMemCode()).toString();
		String sign = SignatrueUtils.getSignToString(text, privateKey);

		params.put("sign", sign);

		System.out.println("请求参数：" + JSON.toJSONString(params));

		String result = HttpUtils.doPost(serviceHost + UrlConst.batchTransfer, params);

		return result;
	}

	/**
	 * 2.2 资金划转
	 * 
	 * @param memberCode  商户编码
	 * @param privateKey  私钥
	 * @param serviceHost 服务host
	 * @param batchNo     批号
	 * @param filePath    文件路径
	 * @return
	 */
	public String batchTransfer(String memberCode, String privateKey, String serviceHost,
			LoadWithRepayPlanReq loadWithRepayPlanReq, File transferFile) {

		if (!transferFile.exists()) {
			return "本地文件不存在";
		}
		Map<String, Object> params = new HashMap<>();
		// params.put("reqStr", JSON.toJSONString(loadWithRepayPlanReq));
		params.put("memCode", loadWithRepayPlanReq.getMemCode());
		params.put("batchNo", loadWithRepayPlanReq.getBatchNo());
		params.put("file", transferFile);
		// 签名
		String text = new StringBuilder().append(loadWithRepayPlanReq.getBatchNo())
				.append(loadWithRepayPlanReq.getFundAccountNo()).append(loadWithRepayPlanReq.getFundAccountType())
				.append(loadWithRepayPlanReq.getMemCode()).toString();
		String sign = SignatrueUtils.getSignToString(text, privateKey);

		params.put("sign", sign);
		String result = HttpUtils.doPost(serviceHost + UrlConst.batchTransfer, params);

		return result;
	}

	/**
	 * 查询开户结果
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param queryOpenAccountReq
	 * @return
	 */
	public String queryOpenAccount(String memberCode, String privateKey, String serviceHost,
			QueryOpenAccountReq queryOpenAccountReq) {

		// 签名
		String sign = SignatrueUtils.getSign(JSON.toJSONString(queryOpenAccountReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}
		queryOpenAccountReq.setSign(sign);

		Map<String, Object> params = new HashMap<>();
		params.put("reqStr", JSON.toJSONString(queryOpenAccountReq));

		String result = HttpUtils.doPost(serviceHost + UrlConst.queryOpenAccount, params);
		return result;
	}

	/**
	 * 2.3 单笔提现
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param cashOutForSelfReq
	 * @return
	 */
	public String cashOutForSelf(String memberCode, String privateKey, String serviceHost,
			CashOutForSelfReq cashOutForSelfReq) {
		// 签名
		String sign = SignatrueUtils.getSign(JSON.toJSONString(cashOutForSelfReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}

		cashOutForSelfReq.setSign(sign);

		Map<String, Object> params = new HashMap<>();
		params.put("reqStr", JSON.toJSONString(cashOutForSelfReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.cashOutForSelf, params);

		return result;
	}

	// 查询虚户余额
	public String queryVritualAccounts(String memberCode, String privateKey, String serviceHost,
			QueryVritualAccountsReq queryVritualAccountsReq) {

		String sign = SignatrueUtils.getSign(JSON.toJSONString(queryVritualAccountsReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}

		queryVritualAccountsReq.setSign(sign);
		Map<String, Object> params = new HashMap<>();
		params.put("reqStr", JSON.toJSONString(queryVritualAccountsReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.queryVritualAccounts, params);
		return result;
	}

	// 查询账户明细
	public String queryAccountingDetails(String memberCode, String privateKey, String serviceHost,
			QueryAccountingDetailsReq queryAccountingDetailsReq) {
		// 签名
		String sign = SignatrueUtils.getSign(JSON.toJSONString(queryAccountingDetailsReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}
		queryAccountingDetailsReq.setSign(sign);
		Map<String, Object> params = new HashMap<>();
		params.put("reqStr", JSON.toJSONString(queryAccountingDetailsReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.queryAccountingDetails, params);
		return result;
	}

	/**
	 * 按批次号查询批次概要信息 2.6
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param queryBatchEssentialsReq
	 * @return
	 */
	public String queryBatchEssentials(String memberCode, String privateKey, String serviceHost,
			QueryBatchEssentialsReq queryBatchEssentialsReq) {

		// 签名
		String sign = SignatrueUtils.getSign(JSON.toJSONString(queryBatchEssentialsReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}
		queryBatchEssentialsReq.setSign(sign);

		Map<String, Object> params = new HashMap<>();
		params.put("reqStr", JSON.toJSONString(queryBatchEssentialsReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.queryBatchEssentials, params);
		return result;
	}

	/**
	 * 4.1 订单上报
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param orderSubmissionReq
	 * @return
	 */
	public String orderSubmission(String memberCode, String privateKey, String serviceHost,
			OrderSubmissionReq orderSubmissionReq) {

		Map<String, Object> params = new HashMap<>();
		String sign = SignatrueUtils.getSign(JSON.toJSONString(orderSubmissionReq), privateKey);

		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}

		orderSubmissionReq.setSign(sign);
		params.put("reqStr", JSON.toJSONString(orderSubmissionReq));

		String result = HttpUtils.doPost(serviceHost + UrlConst.orderSubmission, params);
		return result;

	}

	/**
	 * 4.2 订单状态修改
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param orderChangeStatusReq
	 * @return
	 */
	public String orderChangeStatus(String memberCode, String privateKey, String serviceHost,
			OrderChangeStatusReq orderChangeStatusReq) {
		Map<String, Object> params = new HashMap<>();
		String sign = SignatrueUtils.getSign(JSON.toJSONString(orderChangeStatusReq), privateKey);

		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}
		orderChangeStatusReq.setSign(sign);
		params.put("reqStr", JSON.toJSONString(orderChangeStatusReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.orderChangeStatus, params);
		return result;
	}

	/**
	 * 4.3 支付确认信息
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param confirmPayReq
	 * @return
	 */
	public String confirmPay(String memberCode, String privateKey, String serviceHost, ConfirmPayReq confirmPayReq) {

		Map<String, Object> params = new HashMap<>();

		String sign = SignatrueUtils.getSign(JSON.toJSONString(confirmPayReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}
		confirmPayReq.setSign(sign);
		params.put("reqStr", JSON.toJSONString(confirmPayReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.confirmPay, params);

		return result;

	}

	/**
	 * 4.4 支付退款信息
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param drawbackReq
	 * @return
	 */
	public String drawback(String memberCode, String privateKey, String serviceHost, DrawbackReq drawbackReq) {
		Map<String, Object> params = new HashMap<>();

		String sign = SignatrueUtils.getSign(JSON.toJSONString(drawbackReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}
		drawbackReq.setSign(sign);
		params.put("reqStr", JSON.toJSONString(drawbackReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.drawback, params);
		return result;
	}

	/**
	 * 2.10 单笔资金账户划转
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param singleTransferReq
	 * @return
	 */
	public String singleTransfer(String memberCode, String privateKey, String serviceHost,
			SingleTransferReq singleTransferReq) {

		// 签名
		String text = new StringBuilder().append(singleTransferReq.getFromFundAccountNo())
				.append(singleTransferReq.getMemCode()).append(singleTransferReq.getOccurAmount())
				.append(singleTransferReq.getOrderNo()).append(singleTransferReq.getToFundAccountNo()).toString();
		String sign = SignatrueUtils.getSignToString(text, privateKey);

		singleTransferReq.setSign(sign);

		RequestResult requestResult = HttpUtils.doPost(serviceHost + UrlConst.singleTransfer,
				JSON.toJSONString(singleTransferReq));

		return requestResult.getResult();

	}

	/**
	 * 2.7按批次号查询批次明细
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 * @param queryBatchDetailsReq
	 * @return
	 */
	public String queryBatchDetails(String memberCode, String privateKey, String serviceHost,
			QueryBatchDetailsReq queryBatchDetailsReq) {

		Map<String, Object> params = new HashMap<>();

		String sign = SignatrueUtils.getSign(JSON.toJSONString(queryBatchDetailsReq), privateKey);
		if (null == sign || "".equals(sign)) {
			return "签名异常";
		}
		queryBatchDetailsReq.setSign(sign);
		params.put("reqStr", JSON.toJSONString(queryBatchDetailsReq));
		String result = HttpUtils.doPost(serviceHost + UrlConst.queryBatchDetails, params);

		return result;

	}

}
